package com.ruoyi.hotel.wx.mp.config.service;


import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class WechatTemplateMessageService {

    private static Logger logger = LoggerFactory.getLogger(WechatTemplateMessageService.class);

    @Autowired
    private WxMpService wxMpService;

    /*
    发送微信模板消息
     */
    @Async
    public void sendMsg(String openId,String templateId,String first,String keyword1 ,String keyword2 ,String keyword3 ,String remark,String url){
        List<WxMpTemplateData> wxMpTemplateDataList = Arrays.asList(
                new WxMpTemplateData("first", first, "#000000"),
                new WxMpTemplateData("keyword1", keyword1),
                new WxMpTemplateData("keyword2", keyword2),
                new WxMpTemplateData("keyword3", keyword3),
                new WxMpTemplateData("remark", remark)
        );

        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId(templateId)
                .data(wxMpTemplateDataList)
                .url(url)
                .build();

        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            logger.error("推送失败：" + e.getMessage());
            System.out.println("推送失败：" + e.getMessage());
        }
    }
}
